package com.group01.web.controller;

import com.group01.dto.Address.AddressInsertInfoDTO;
import com.group01.dto.Address.AddressUpdateInfoDTO;
import com.group01.result.Result;
import com.group01.service.AddressService;
import com.group01.vo.Address.AddressGetInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.DecimalMin;
import java.util.List;

/**
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/23
 */
@RestController
@RequestMapping("/address")
@Validated
@Api(tags = "收货地址模块")
public class AddressController {

    /**
     * 注入
     */
    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    /**
     * 获取用户的所有收货地址
     * @return {@link Result }<{@link List }<{@link AddressGetInfoVO }>>
     */
    @ApiOperation("获取用户的所有收货地址")
    @GetMapping
    public Result<List<AddressGetInfoVO>> getAddressList(){
        return addressService.getAddressList();
    }

    /**
     * 新增收货地址
     * @param addressInsertInfoDTO 地址传输对象DTO
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("新增收货地址")
    @PostMapping
    public Result<String> addAddress(@Validated @RequestBody AddressInsertInfoDTO addressInsertInfoDTO) {
        return addressService.addAddress(addressInsertInfoDTO);
    }

    /**
     * 修改收货地址
     * @param addressUpdateInfoDTO 地址传输对象DTO
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("修改收货地址")
    @PutMapping
    public Result<String> updateAddress(@Validated @RequestBody AddressUpdateInfoDTO addressUpdateInfoDTO) {
        return addressService.updateAddress(addressUpdateInfoDTO);
    }

    /**
     * 设置用户默认收货地址
     * @param addressId 地址ID
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/setDefault/{addressId}")
    public Result<String> setDefault(
            @PathVariable("addressId") @DecimalMin(value = "0" , message = "地址id是数字且大于0") Integer addressId
    ) {
        return addressService.setDefault(addressId);
    }


    /**
     * 取消用户默认收货地址
     * @param addressId 地址ID
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("取消默认地址")
    @PutMapping("/cancelDefault/{addressId}")
    public Result<String> cancelDefault(
            @PathVariable("addressId") @DecimalMin(value = "0" , message = "地址id是数字且大于0") Integer addressId
    ) {
        return addressService.cancelDefault(addressId);
    }

    /**
     * 删除收货地址
     * @param id 地址id
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("删除收货地址")
    @DeleteMapping
    public Result<String> deleteAddress(
            @RequestParam("id") @DecimalMin(value = "0" , message = "id是数字且大于0") Integer id
    ) {
        return addressService.deleteAddress(id);
    }
}
